#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define BUF_SIZE 30

int main(int arg,char  * argvs[])
{
    int clnt_sock;
    char message[BUF_SIZE];
    
    struct sockaddr_in serv_adr,from_adr;
    clnt_sock = socket(PF_INET,SOCK_DGRAM,0);
    memset(&serv_adr,0,sizeof(serv_adr));
    serv_adr.sin_family = AF_INET;
    serv_adr.sin_addr.s_addr = inet_addr(argvs[1]);
    serv_adr.sin_port = htons(atoi(argvs[2]));

    socklen_t socklen;
    
    while (1)
    {
        fputs("Insert message(q to quit):",stdout);
        fgets(message,BUF_SIZE,stdin);
        //printf("%s",message);
        if(!strcmp(message,"q\n")||!strcmp(message,"Q\n"))
        {
            fputs("quit!\n",stdout);
            break;
        }
        //fputs("round\n",stdout);
        sendto(clnt_sock,message,sizeof(message),0,(struct sockaddr *)&serv_adr,sizeof(serv_adr));
        socklen = sizeof(from_adr);
        int lenstr = recvfrom(clnt_sock,message,BUF_SIZE,0,(struct sockaddr *)&from_adr,&socklen);
        message[lenstr] = 0;
        printf("Message from server : %s",message);
    }
    close(clnt_sock);
    return 0;
}
